package com.ingenio.siot.adapter;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.ingenio.siot.Adapter;
import com.ingenio.siot.entity.TipoIntervalo;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;

public class TipoIntervaloAdapter extends Adapter{
	
	public List<TipoIntervalo> devolverTodos() throws Exception{
		Connection conn = null;
		PreparedStatement stmt = null;
    	List<TipoIntervalo> tipoIntevalos = new ArrayList<TipoIntervalo>();
    	
		try {
    		conn = (Connection) abrirConexion();
    		String sel ="SELECT * FROM tiposintervalos";
    		stmt = (PreparedStatement) conn.prepareStatement(sel);
    		    		
    		ResultSet result = stmt.executeQuery();
    		TipoIntervalo ti = null;
    		while(result.next()){
    			Integer idTipoIntervalo = result.getInt("IdTipoIntervalo");
    			String descTipoIntervalo = result.getString("descTipoInvetalo");
    			String condicion = result.getString("condicion");
    			ti = new TipoIntervalo(idTipoIntervalo, descTipoIntervalo, condicion);
    			tipoIntevalos.add(ti);
    		}
    	}
    	catch (Exception e) {
			e.printStackTrace();
			throw new Exception("Error al conectar con la base de datos.", e);
		}
    	finally{
    		cerrarConexion(conn);
    	}
		return tipoIntevalos;
	}
	
	public TipoIntervalo devolverTipoIntervalo(Integer idTipoIntervalo) throws Exception{
		Connection conn = null;
		PreparedStatement stmt = null;
    	TipoIntervalo ti = null;
    	try {
    		conn = (Connection) abrirConexion();
    		String sel ="SELECT * FROM tiposintervalos WHERE IdTipoIntervalo = ? ";
    		stmt = (PreparedStatement) conn.prepareStatement(sel);
    		stmt.setInt(1, idTipoIntervalo);
    		ResultSet result = stmt.executeQuery();
    		if(result.next()){
    			String descTipoIntervalo = result.getString("descTipoIntervalo");
    			String condicion = result.getString("condicion");
    			ti = new TipoIntervalo(idTipoIntervalo, descTipoIntervalo, condicion);
    		}
    	}
    	catch (Exception e) {
			e.printStackTrace();
			throw new Exception("Error al conectar con la base de datos.", e);
		}
    	finally{
    		cerrarConexion(conn);
    	}
		return ti;
	}

}
